package com.ihr360.payroll.service.attendanceplan.impl;

import com.ihr360.payroll.dao.OvertimeRuleRepository;
import com.ihr360.payroll.model.dto.attendance.rule.overtime.OvertimeRule;
import com.ihr360.payroll.model.dto.attendance.rule.overtime.SyntheticalOverTimeRule;
import com.ihr360.payroll.service.attendanceplan.OvertimeRuleService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tomtao
 */
@Service
public class OvertimeRuleServiceImpl extends AbstractSimpleRestServiceImpl<OvertimeRule, Long> implements OvertimeRuleService {
    @Autowired
    OvertimeRuleRepository overtimeRuleRepository;

    @Override
    public SimpleRestReposity<OvertimeRule, Long> getSimpleRestReposity() {
        return overtimeRuleRepository;
    }

    @Override
    public OvertimeRule save(OvertimeRule overtimeRule) {
        if(overtimeRule.getId() != null) {
            OvertimeRule ret = overtimeRuleRepository.findOne(overtimeRule.getId());
            if(ret == null) {
                return null;
            }
            ret.setNumOpRatio(overtimeRule.getNumOpRatio());
            ret.setHolidayRatio(overtimeRule.getHolidayRatio());
            ret.setBaseRatio(overtimeRule.getBaseRatio());
            ret.setFixedBase(overtimeRule.getFixedBase());
            ret.setWeekendRatio(overtimeRule.getWeekendRatio());
            SyntheticalOverTimeRule rule = overtimeRule.getRule();
            if(rule != null) {
                rule.setRule(ret);
                ret.setRule(rule);
            }
            ret.setBasementType(overtimeRule.getBasementType());
            ret.setReferenceFields(overtimeRule.getReferenceFields());
            ret.setRemark(overtimeRule.getRemark());
            ret.setUpdatedAt(new Date());
            return overtimeRuleRepository.save(ret);
        }
        return overtimeRuleRepository.save(overtimeRule);
    }

}
